import axios, { AxiosRequestConfig, AxiosResponse } from 'axios'
import { message } from 'antd'
import NproGress from 'nprogress'
import whiteList from './whiteList'
import 'nprogress/nprogress.css'


const http = axios.create({
    // 公共地址
    baseURL: "http://localhost:3333/admin",
    // 超时
    timeout: 10000
})

// 请求拦截
http.interceptors.request.use((config: any) => {
    // console.log(config)
    let token = sessionStorage.getItem('token')? sessionStorage.getItem('token'): ''
    config.headers.Authorization = token
    NproGress.start()
    return config
})

// 响应拦截
http.interceptors.response.use((data: AxiosResponse) => {
    NproGress.done()
    const filterWhiteList = whiteList.filter(item => item.path === data.config.url && item.methods === data.config.method)
    filterWhiteList.forEach(item => {
        if (item.path === data.config.url && item.methods === data.config.method) {
        } else {
            message.success(data.data.message)
        }
    })
    
    return data.data
}, (error) => {
    message.error(error.response.data.message)
})

export default http